package com.jlf.app.authorization.controller;

import com.jlf.app.authorization.entity.TestEntityOutput;
import com.jlf.core.annotation.restful.controller.RestfulController;
import com.jlf.core.annotation.restful.method.RestfulGet;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 测试接口
 * String testString，在webflux下，不需要设置produces
 *
 * @author wujr
 * 2023/8/2
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/8/2 1.0 新增]
 */
@Tag(name = "测试接口")
@Slf4j
@RestfulController(path = "/rest/app/authorization/test", name = "测试接口")
public class TestController {
    @RestfulGet(path = "/mono/string", code = "testMonoString", name = "响应式测试字符串")
    public Mono<String> testMonoString(){
        return Mono.just("Hello World!");
    }
    @RestfulGet(path = "/string", code = "testString", name = "测试字符串")
    public String testString(){
        return "Hello World!";
    }

    @RestfulGet(path = "/mono/int", code = "testMonoInt", name = "响应式测试整数")
    public Mono<Integer> testMonoInt(){
        return Mono.just(2023);
    }
    @RestfulGet(path = "/int", code = "testInt", name = "测试整数")
    public Integer testInt(){
        return 2023;
    }

    @RestfulGet(path = "/flux/int", code = "testFluxInt", name = "响应式测试数组整数")
    public Flux<Integer> testFluxInt(){
        return Flux.fromArray(new Integer[]{2020,2021,2022});
    }
    @RestfulGet(path = "/array/int", code = "testIntArray", name = "测试数组array")
    public Integer[] testIntArray(){
        return new Integer[]{2020,2021,2022,2023};
    }
    @RestfulGet(path = "/list/int", code = "testIntList", name = "测试数组list")
    public List<Integer> testIntList(){
        return Arrays.asList(2020,2021,2022,2023);
    }
    @RestfulGet(path = "/mono/entity", code = "testEntityMono", name = "响应式测试对象")
    public Mono<TestEntityOutput> testEntityMono(){
        TestEntityOutput output = new TestEntityOutput();
        output.setName("Hello World!");
        output.setAge(2022);
        output.setBirthday(LocalDateTime.now());
        return Mono.just(output);
    }
    @RestfulGet(path = "/entity", code = "testEntity", name = "测试对象")
    public TestEntityOutput testEntity(){
        TestEntityOutput output = new TestEntityOutput();
        output.setName("Hello World!");
        output.setAge(2023);
        output.setBirthday(LocalDateTime.now());
        return output;
    }
    @RestfulGet(path = "/exp", code = "testException", name = "测试异常")
    public Mono<Void> testException() throws Exception{
        throw new Exception("抛出异常了！！！");
    }
    @RestfulGet(path = "/mono/void", code = "testMonoVoid", name = "响应式无返回")
    public Mono<Void> testMonoVoid() {
        return Mono.empty();
    }
    @RestfulGet(path = "/void", code = "testVoid", name = "无返回")
    public void testVoid() {
    }
    @RestfulGet(path = "/consumer/exp", code = "testConsumerException", name = "测试消费时抛出异常")
    public Mono<Integer> testConsumerException() throws Exception{
        return Mono.defer(() -> Mono.just(new Date())).map(Date::getTime)
                .map((v) -> v / 0)//new Random().nextInt(2))
                .map(Long::intValue);
    }

    @RestfulGet(path = "/random/int", code = "testRandomInt", name = "随机数字")
    public Mono<Integer> testRandomInt(){
        Integer v = new Random().nextInt(2) == 1 ? null : (int)new Date().getTime();
        return Mono.justOrEmpty(v).switchIfEmpty(Mono.defer(() -> Mono.error(new NullPointerException("空指针"))));
    }
}
